package com.useeinfo.oa.modules.base.dao;

import com.useeinfo.framework.extend.dao.CrudDao;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.QueryUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.base.entity.FacilityCat;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: bin.liu
 * @Date: 2018/12/5 10:44
 * @Description: 设备类型dao
 */
@Repository("facilityCatDao")
public class FacilityCatDao implements CrudDao<FacilityCat> {

    private final static Logger logger = LoggerFactory.getLogger(FacilityCatDao.class);

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public Map<String, Object> getSearchCondition(Map<String, String> queryHash){
        Map<String, Object> conditionHash = new HashMap<>();

        conditionHash.put("fc.deleteStatus = ?{paramIndex} ", false);
        if (queryHash == null || queryHash.size() == 0) {
            return conditionHash;
        }
        String searchCode = queryHash.get("searchCode");
        if (!StringUtils.isEmpty(searchCode)) {
            conditionHash.put("(fc.catName like ?{paramIndex}) ", "%" + searchCode + "%");
        }
        String catName = queryHash.get("catName");
        if (!StringUtils.isEmpty(catName)) {
            conditionHash.put("(fc.catName = ?{paramIndex}) ", catName);
        }
        String bigParent = queryHash.get("bigParent");//查专业(根节点)
        if (!StringUtils.isEmpty(bigParent)) {
            conditionHash.put("(fc.parentId is null or fc.parentId = ?{paramIndex}) ", 0L);
        }
        String notBigParent = queryHash.get("notBigParent");//查类型(不能选择根节点)
        if (!StringUtils.isEmpty(notBigParent)) {
            conditionHash.put("(fc.parentId is not null and 1 = ?{paramIndex}) ", 1);
        }
        String lvl = queryHash.get("lvl");
        if (!StringUtils.isEmpty(lvl)) {
            conditionHash.put("(fc.lvl = ?{paramIndex}) ", StringConverters.ToInteger(lvl));
        }
        String parentId = queryHash.get("parentId");
        if (!StringUtils.isEmpty(parentId)) {
            conditionHash.put("(fc.parentId = ?{paramIndex}) ", StringConverters.ToLong(parentId));
        }
        String ancestryId = queryHash.get("ancestryId");
        if (!StringUtils.isEmpty(ancestryId)) {
            conditionHash.put("(fc.parentIds like ?{paramIndex} or fc.parentIds like '%,"+ancestryId+",%' or fc.parentIds = '"+ancestryId+"' or fc.parentIds like '%,"+ancestryId+"') ", ancestryId+",%");
        }
        String rootParentId = queryHash.get("rootParentId");//查所属根节点
        if (!StringUtils.isEmpty(rootParentId)) {
            conditionHash.put("(fc.parentIds like ?{paramIndex} or fc.parentIds = '"+rootParentId+"') ", rootParentId+",%");
        }
        String enterpriseId = queryHash.get("enterpriseId");
        if (!StringUtils.isEmpty(enterpriseId)) {
            conditionHash.put("(fc.enterprise.id = ?{paramIndex}) ", StringConverters.ToLong(enterpriseId));
        }
        // 查询同专业分类下是否有重名的分类名称
        String rootCatId = queryHash.get("rootCatId");
        String nullParentId = queryHash.get("nullParentId");
        if (!StringUtils.isEmpty(rootCatId)){
            conditionHash.put("(fc.parentIds = "+ rootCatId +" or fc.parentIds like ?{paramIndex}) ", rootCatId+",%");
        } else if (!StringUtils.isEmpty(nullParentId)){
            conditionHash.put("(fc.parentId is null and 1 = ?{paramIndex}) ", 1);
        }
        String rootCatName = queryHash.get("rootCatName");//根据名称查专业(根节点)
        if (!StringUtils.isEmpty(rootCatName)) {
            conditionHash.put("(fc.catName like ?{paramIndex} and ( fc.parentId is null or fc.parentId = 0)) ", "%" + rootCatName + "%");
        }

        String magicLike = queryHash.get("magicLike");
        if (StringUtils.isNotBlank(magicLike)) {
            conditionHash.put("fc.catName like ?{paramIndex} ", "%" + magicLike + "%");
        }
        String magicId = queryHash.get("magicId");
        if (StringUtils.isNotBlank(magicId)) {
            conditionHash.put("fc.id != ?{paramIndex} ", StringConverters.ToLong(magicId));
        }
        return conditionHash;
    }


    /**
     * 获取总记录数
     */
    @Override
    public Long totalRecord(Map<String, String> queryHash){
        Map<String, Object> conditions = getSearchCondition(queryHash);
        TypedQuery<Long> typedQuery = QueryUtils.getTypedQueryByCondition("select count(fc) from FacilityCat fc ", conditions, entityManager, Long.class);
        return typedQuery.getSingleResult();
    }

    /**
     * 分页列表
     */
    @Override
    public List<FacilityCat> findList(QueryParam queryParam){
        String sqlInfo = queryParam.joinJPQL("select fc from FacilityCat fc ", "order by fc.id desc ");
        Map<String, Object> conditions = getSearchCondition(queryParam.getSqlMap());
        TypedQuery<FacilityCat> typedQuery = QueryUtils.getTypedQueryByCondition(sqlInfo, conditions, entityManager, FacilityCat.class);
        return queryParam.findPageList(typedQuery);
    }

    /**
     * id获取记录
     */
    @Override
    public FacilityCat findModel(Long id){
        return entityManager.find(FacilityCat.class, id);
    }

    /**
     * 增加记录
     */
    @Override
    public Integer add(FacilityCat facilityCat){
        facilityCat.setCreateDate(new Date());
        entityManager.persist(facilityCat);
        logger.info("FacilityCatDaoImpl添加facilityCat成功！");
        return 1;
    }

    /**
     * 修改记录
     */
    @Override
    public Integer update(FacilityCat facilityCat){
        facilityCat.setUpdateDate(new Date());
        entityManager.merge(facilityCat);
        return 1;
    }

    /**
     * 删除记录
     */
    @Override
    public Integer delete(Long id){
        FacilityCat facilityCat = entityManager.find(FacilityCat.class, id);
        entityManager.remove(facilityCat);
        return 1;
    }

    /**
     * 删除记录 - 逻辑删除
     */
    @Override
    public Integer deleteLogic(FacilityCat facilityCat){
        facilityCat.setDeleteStatus(true);
        return 1;
    }
}
